<?php

/**
 * @author honestphan
 * @copyright 2012
 */
 
 //G?i t?p tin giao di?n trong thu m?c view/theme/template tuong ?ng
include('model/m_canbo.php');
include('controller/c_thongke.php');
$page = $_GET['act'];
 switch($page){
    case 'list':
    
     if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
     
      if(!$_SESSION['type']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
     
     
    //Get language
    require('language/canbo/list_canbo.php');
    
    $title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
    $active = active;
     $deactive = deactive;
    $cot_ten = ten_can_bo;
    $cot_dan_toc = dan_toc;
    $cot_chuc_danh = chuc_danh;
    $cot_phong_ban = phong_ban;
    $cot_ngay_sinh = ngay_sinh;
    $cot_dien_thoai_nha_rieng = dien_thoai_nha_rieng;
    $cot_dien_thoai_co_quan = dien_thoai_co_quan;
    $cot_email = email;
    $cot_di_dong = di_dong;
    
    $cot_thao_tac = thao_tac;
    $link_add ='index.php?module=canbo&act=add';

     if($_POST['selected']==""){
        $err['exist'] = chua_chon;
        $error_name = $err['exist'];
        }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    
    if($_REQUEST['page']==""){ $page = 1;}
    else{$page = $_REQUEST['page'];}
    
    //get input for class page
      $limit = 10;
      $start = ($page - 1) * $limit; 
      
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
        delCanbo($id);
      }  
    $_SESSION['success'] = del_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);
     }

    $list_canbo = getListCanbo($start, $limit);
  
    $total = total();
    $paging = new Pagination();
    $paging->total = $total;
    $paging->limit = $limit;
    $paging->url = "index.php?module=canbo&act=list&page={page}";
    $paging->page = $page;
    $pagination = $paging->render();
    $heading_title = heading_title." (".$total." cán bộ)";

    require('view/template/canbo/list_canbo.tpl');  
    break;
    
    //case them can bo
    case 'add':
    
     if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
     
    if($_SESSION['type']=='cb'){
         header('LOCATION: index.php?module=home');  
         exit();
    }
     require ('language/canbo/add_canbo.php');
     $heading_title = heading_title;
     $title = heading_title;
     $ma_can_bo = ma_can_bo;
     $ten_can_bo = ten_can_bo;
     $ngay_sinh = ngay_sinh;
     $ten_dang_nhap = ten_dang_nhap;
     $mat_khau = mat_khau;
     $xac_nhan_mat_khau = xac_nhan_mat_khau;
     $chuc_danh = chuc_danh;
     $phong_ban = phong_ban;
     $dia_chi = dia_chi;
     $dien_thoai_noi_bo = dien_thoai_noi_bo;
     $dien_thoai_co_quan = dien_thoai_co_quan;
     $dien_thoai_nha_rieng = dien_thoai_nha_rieng;
     $di_dong = di_dong;
     $active = active;
     $deactive = deactive;
     
     $_SESSION['fax'] = $_POST['fax'];
     $fax = fax;
     $email = email;
     $trang_thai = trang_thai;
     $dan_toc = dan_toc;
     
     $list_chucdanh = getListChucdanh();
     $list_phongban = getListPhongban();
     $list_dantoc = getListDantoc();
     $link_list = 'index.php?module=canbo&act=list';
      
      
      $_SESSION['tendangnhap'] = $_POST['ten_dang_nhap'];
      
   
       if($_POST['ten_dang_nhap']==""){
        $err['exist'] = ten_dang_nhap1;
        $error_dn = $err['exist'];
        
       }
       //kiem tra su ton tai
     $dn = check_dn(trim($_POST['ten_dang_nhap']));
     if($dn == '1'){
        $err['exist'] = da_ton_tai;
        $error_dn = $err['exist'];
     }
     //kiem tra du lieu
     $_SESSION['tencanbo'] = $_POST['ten_can_bo'];
    if($_POST['ten_can_bo']==""){
        $err['exist'] = chua_nhap_ten;
        $error_name = $err['exist'];
        
    }
    $_SESSION['ngaysinh'] = $_POST['ngay_sinh'];
    
   // if($_POST['ngay_sinh']=="")
    //{
    //    $err['exist'] = nhap_ngay_sinh;
    //    $error_ns = $err['exist'];
   // }
      
    
        if($_POST['mat_khau']==""){
        $err['exist'] = yeu_cau_phai_nhap;
        $error_mk = $err['exist'];
        
    }
      if($_POST['mat_khau']!=$_POST['xac_nhan_mat_khau']){
        $err['exist'] = mat_khau_xac_nhan_khong_dung;
        $error_mk1 = $err['exist'];
    }
   
    /**
      
    $_SESSION['chucdanh'] = $_POST['chuc_danh_id'];
        if($_POST['chuc_danh_id']=="0"){
        $err['exist'] = chon_chuc_danh;
        $error_chucdanh = $err['exist'];
    }
    $_SESSION['phongban'] = $_POST['phong_ban_id'];
        if($_POST['phong_ban_id']=="0"){
        $err['exist'] = chon_phong_ban;
        $error_phongban = $err['exist'];
    
    }
  
   $_SESSION['diachi'] = $_POST['dia_chi'];
        if($_POST['dia_chi']==""){
        $err['exist'] = Nhap_dia_chi;
        $error_diachi = $err['exist'];
    }
    $_SESSION['dtnoibo'] = $_POST['dien_thoai_noi_bo'];
        if($_POST['dien_thoai_noi_bo']==""){
        $err['exist'] = Nhap_dien_thoai_noi_bo;
        $error_noibo = $err['exist'];
    }
    $_SESSION['dtcoquan'] = $_POST['dien_thoai_co_quan'];
        if($_POST['dien_thoai_co_quan']==""){
        $err['exist'] = Nhap_dien_thoai_co_quan;
        $error_coquan = $err['exist'];
        
    }
    $_SESSION['dtnharieng'] = $_POST['dien_thoai_nha_rieng'];
        if($_POST['dien_thoai_nha_rieng']==""){
        $err['exist'] = Nhap_dien_thoai_nha_rieng;
        $error_nharieng = $err['exist'];
    }
    $_SESSION['dd'] = $_POST['di_dong'];
        if($_POST['di_dong']==""){
        $err['exist'] = nhap_so_dien_thoai_di_dong;
        $error_dd = $err['exist'];
        
    }
    $_SESSION['dantoc'] = $_POST['dan_toc_id'];
        if($_POST['dan_toc_id']=="0"){
        $err['exist'] = chon_dan_toc;
        $error_dantoc = $err['exist'];
    }
    $_SESSION['email'] = $_POST['email'];
    if($_POST['email']==""){
        $err['exist'] = nhap_dia_chi_email;
        $error_email = $err['exist'];
    }
       //kiem tra su ton tai
     $dn = check_email(trim($_POST['email']));
     if($dn == '1'){
        $err['exist'] = da_ton_tai;
        $error_email = $err['exist'];
     }
     **/
     
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
    insertCanbo($_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    
 
    require('view/template/canbo/add_canbo.tpl');
    break;
    
    //case them can bo
    case 'edit':
   
    if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
    }
    
    if($_SESSION['type']=='cb'){
         header('LOCATION: index.php?module=home');  
         exit();
    }
   
    
     require ('language/canbo/edit_canbo.php');
     $title = heading_title;
     $heading_title = heading_title;
     $ma_can_bo = ma_can_bo;
     $ten_can_bo = ten_can_bo;
     $ngay_sinh = ngay_sinh;
     $ten_dang_nhap = ten_dang_nhap;
     $mat_khau = mat_khau;
     $xac_nhan_mat_khau = xac_nhan_mat_khau;
     $chuc_danh = chuc_danh;
     $phong_ban = phong_ban;
     $dia_chi = dia_chi;
     $dien_thoai_noi_bo = dien_thoai_noi_bo;
     $dien_thoai_co_quan = dien_thoai_co_quan;
     $dien_thoai_nha_rieng = dien_thoai_nha_rieng;
     $di_dong = di_dong;
     $fax = fax;
     $email = email;
     $trang_thai = trang_thai;
     $dan_toc = dan_toc;
     $active = active;
     $deactive = deactive;
    $list_chucdanh = getListChucdanh();
    $list_phongban = getListPhongBan(); 
    $list_dantoc = getListDantoc();
    $link_list = 'index.php?module=canbo&act=list';
    $id = $_REQUEST['id'];
    $canbo = getCanbo($id);
    
      //kiem tra su ton tai
     $dn = check_exist_local(trim($_POST['ten_can_bo']));
     if($dn == '1'){
        $err['exist'] = da_ton_tai;
        $error_name = $err['exist'];
     }
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
    
    editCanbo($id, $_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    require('view/template/canbo/edit_canbo.tpl');
    break;
    
    //update my prolife
    
    case 'my_prolife':
   
     if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
    
     require ('language/canbo/edit_myprolife.php');
     $title = heading_title;
     $heading_title = heading_title;
     $ma_can_bo = ma_can_bo;
     $ten_can_bo = ten_can_bo;
     $ngay_sinh = ngay_sinh;
     $ten_dang_nhap = ten_dang_nhap;
     $mat_khau = mat_khau;
     $xac_nhan_mat_khau = xac_nhan_mat_khau;
     $chuc_danh = chuc_danh;
     $phong_ban = phong_ban;
     $dia_chi = dia_chi;
     $dien_thoai_noi_bo = dien_thoai_noi_bo;
     $dien_thoai_co_quan = dien_thoai_co_quan;
     $dien_thoai_nha_rieng = dien_thoai_nha_rieng;
     $di_dong = di_dong;
     $fax = fax;
     $email = email;
     $trang_thai = trang_thai;
     $dan_toc = dan_toc;
     $active = active;
     $deactive = deactive;
    $list_chucdanh = getListChucdanh();
    $list_phongban = getListPhongBan(); 
    $list_dantoc = getListDantoc();
    $link_list = 'index.php?module=canbo&act=my_prolife';
    
    $id = $_SESSION['can_bo_id'];
    
    $canbo = getCanbo($id);
    
      //kiem tra su ton tai
     $dn = check_exist_local(trim($_POST['ten_dang_nhap']));
     if($dn == '1'){
        $err['exist'] = da_ton_tai;
        $error_name = $err['exist'];
     }
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
    $_POST['status'] ='1';
    editCanbo($id, $_POST);
    $success = edit_success;
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);
    exit();    
    }
    require('view/template/canbo/my_prolife.tpl');
    break;
    
    
    
    case 'del':
    get_file('language/canbo/del_canbo.php');
    $link_list = 'index?module=canbo&act=list';
    $id = $_REQUEST['id'];
    delCanbo($id);
    $_SESSION['success'] = del_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);  
    exit();
    break;
    
    /**
     * 
     * This case was wiriten by ductoan
     * For member login to system
     * */
     
    case 'login':
    require ('language/canbo/login.php');
  
    $title = login;
    $t_username = user_name;
    $t_password = pass;
    $button_login = login;
    $text_login = login_to_control_system;
    $link_home = 'index.php?module=home';
     
    if(($_POST['uname']=="")&&($_POST['pwd']=="")){
        $err['exist'] = yeu_cau_dang_nhap;
        $error_mk = $err['exist'];
    } else{
            
            if($_POST['uname']==""){
            $err['exist'] = nhap_tk;
            $error_username = $err['exist'];
            }
            if($_POST['pwd']==""){
                $err['exist'] = nhap_mk;
                $error_mk = $err['exist'];
            } 
        
    }
    
    //$_SESSION['user_name'] = $_POST['uname'];
    
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
   
    $pwd = trim($_POST['pwd']);
    $u = trim($_POST['uname']);
   
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
    //hash('sha256','mk');
    //$login_check = login($u,$pwd);
    $login_check = check_login($u,hash('sha256',$pwd));
   
    //echo $login_check;
    
    if($login_check !='0'){
    $canbo = getAdmin($u,hash('sha256',$pwd));
    $_SESSION['success'] = login_success; 
    $_SESSION['loged'] = md5($canbo['ten_dang_nhap']);  
    $_SESSION['ten_can_bo'] = $canbo['ten_can_bo'];
    $_SESSION['type'] = $canbo['cap_bac'];
    $_SESSION['user'] = $canbo['ten_dang_nhap'];
    $_SESSION['can_bo_id'] = $canbo['can_bo_id'];
     //chuyen huong ve home
    header('LOCATION: '.$link_home);    
    exit();   
    } else{
    
    $error_username = thong_tin_dang_nhap_khong_dung;  
        
    }
   
   }
    
    require('view/template/canbo/login_form.tpl');
    break;
    
    /**
     * 
     * case logout
     * 
     * */
    
    
    case 'logout':
    $_SESSION['success'] = ""; 
    $_SESSION['loged'] = "";
    $_SESSION['ten_can_bo'] = "";
    $_SESSION['type'] = "";
    session_destroy();
    mysql_close(dbconnect());
    
    header('LOCATION: index.php?module=canbo&act=login');  
    exit();
    
    break;

 }
 
?>